﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient;
using System.Collections.ObjectModel;
using IndexMarkSilverlightClient.IndexMarkServiceReference;

namespace IndexMarkSilverlightClient.Views
{
    public partial class IndexSettings : ChildWindow
    {
        private int flagComplited = 0;

        public bool ISItemVisible
        {
            set
            {
                if (value)
                {
                    this.ISItem.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    this.ISItem.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        public bool USTUItemVisible
        {
            set
            {
                if (value)
                {
                    this.USTUItem.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    this.USTUItem.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        public bool CatsItemVisible
        {
            set
            {
                if (value)
                {
                    this.CatsItem.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    this.CatsItem.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        public bool SettingsItemVisible
        {
            set
            {
                if (value)
                {
                    this.SettingsItem.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    this.SettingsItem.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }
        
        public IndexSettings()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(IndexSettings_Loaded);
        }

        void IndexSettings_Loaded(object sender, RoutedEventArgs e)
        {
            tabControl1.Visibility = Visibility.Collapsed;
            OKButton.Visibility = Visibility.Collapsed;
            CancelButton.Visibility = Visibility.Collapsed;
            flagComplited = 0;
            ISTree.IDTerm = this.CurrentTermID;
            USTU.IDTerm = this.CurrentTermID;
            CategoriesCtrl.TermID = this.CurrentTermID;

            CategoriesCtrl.LoadCompleted += new EventHandler(CategoriesCtrl_LoadCompleted);
            USTU.LoadCompleted += new EventHandler(USTU_LoadCompleted);
            ISTree.LoadCompleted +=new EventHandler(ISTree_LoadCompleted);

            
            ISTree.LoadTree();
            USTU.LoadTree();
            CategoriesCtrl.LoadCats();
            
            //tabControl1.Visibility = Visibility.Visible;
            //OKButton.Visibility = Visibility.Visible;
            //CancelButton.Visibility = Visibility.Visible;
            //loadingBar.Visibility = Visibility.Collapsed;

        }



        void CategoriesCtrl_LoadCompleted(object sender, EventArgs e)
        {
            flagComplited++;
            loadComplite();


        }

        void USTU_LoadCompleted(object sender, EventArgs e)
        {
            flagComplited++;
            loadComplite();
        }


        void loadComplite()
        {

            if (flagComplited == 2)
            {
                tabControl1.Visibility = Visibility.Visible;
                OKButton.Visibility = Visibility.Visible;
                CancelButton.Visibility = Visibility.Visible;
                loadingBar.Visibility = Visibility.Collapsed;
            }
        }

        void ISTree_LoadCompleted(object sender, EventArgs e)
        {
            flagComplited++;
            loadComplite();

        }



        public int CurrentTermID
        {
            get;
            set;
        }

        public ObservableCollection<Category> SelectedCategories
        {
            get
            {
                var lst = new ObservableCollection<Category>();
                foreach (var category in CategoriesCtrl.Categories)
                {
                    if (category.Check) lst.Add(category);
                }
                return lst;
            }
        }

        public ObservableCollection<Respondent> SelectedRespondents
        {
            get
            {
                var lst = new ObservableCollection<Respondent>();
                foreach (var teaher in USTU.Teahers)
                {
                    if (teaher.IsSelected) lst.Add(teaher);
                }
                return lst;
            }
        }

        public ObservableCollection<Section> SelectedSections
        {
            get
            {
                var lst = new ObservableCollection<Section>();
                foreach (var section in ISTree.Sections)
                {
                    if (section.Check) lst.Add(section);
                }
                return lst;
            }
        }

        public ObservableCollection<Characteristic> SelectedCharacteristics
        {
            get
            {
                var lst = new ObservableCollection<Characteristic>();
                foreach (var characteristic in ISTree.Characteristics)
                {
                    if (characteristic.Check) lst.Add(characteristic);
                }
                return lst;
            }
        }

        public IEnumerable<int> Categories
        {
            get
            {
                
                ObservableCollection<int> list = new ObservableCollection<int>();
                foreach (Category Cat in CategoriesCtrl.Categories) list.Add(Cat.IDCategory);
                return list;
            }
        }


        public IEnumerable<int> Respondents
        {
            get 
            {
                ObservableCollection<int> list = new ObservableCollection<int>();
                foreach (Respondent T in USTU.BlackTeahers) list.Add(T.Person.IDPerson);
                return list;
            }
        }

        public IEnumerable<int> Characteristics
        {
            get
            {
                ObservableCollection<int> list = new ObservableCollection<int>();
                foreach (Characteristic CT in ISTree.Characteristics) list.Add(CT.ID);
                return list;
            }
        }

        

        public IndexListType TypeReport
        {
            get
            {
                if ((bool)CompactRB.IsChecked)
                {
                    return IndexListType.Compact;
                }
                if ((bool)FullRB.IsChecked)
                {
                    return IndexListType.Full;
                }
                return IndexListType.Compact;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

